package com.mty.cc.controller;


import com.mty.cc.po.Admin;
import com.mty.cc.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
 * @description: 控制层
 * @author: mty
 */
@Controller
@RequestMapping(value = "/admin")
class AdminController {


    @Autowired(required=false)
    private AdminService adminService;

    /*
     * @description: 跳转到首页
     * @author: mty
     */
    @RequestMapping("/adminIndex")
    public String adminIndex(HttpServletRequest request,Model model) throws Exception{
        List<Admin> adminList = adminService.queryByAll();
        int total = adminList.size();
        model.addAttribute("adminList", adminList);
        model.addAttribute("total", total);
        return "admin/AdminIndex";
    }

    /*
     * @description:进入修改
     * @author: mty
     */
    @RequestMapping("/adminEdit/{id}")
    public ModelAndView  adminEdit(@PathVariable("id") String id,Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        Admin admin = adminService.queryById(id);
        model.addAttribute("admin", admin);
        mv.setViewName("admin/AdminEdit");
        return mv;
    }

    /*
     * @description:修改
     * @author: mty
     */
    @RequestMapping("/adminEditSubmit")
    public String  adminEditSubmit(Admin admin,Model model,HttpServletRequest request) throws Exception{
        try{
            adminService.update(admin);
            request.setAttribute("msg", "修改成功！");
            return "admin/AdminEdit";
        }catch (Exception e){
            request.setAttribute("msg", "修改失败！");
            return "admin/AdminEdit";
        }
    }

    /*
     * @description:进入添加
     * @author: mty
     */
    @RequestMapping("/adminAdd")
    public String  adminAdd(Model model,HttpServletRequest request) throws Exception{
        return "admin/AdminAdd";
    }

    /*
     * @description:增加
     * @author: mty
     */
    @RequestMapping("/adminAddSub")
    public String  adminAddSub(Admin admin,Model model,HttpServletRequest request) throws Exception{
        try{
            adminService.add(admin);
            request.setAttribute("msg", "添加成功！");
            return "admin/AdminAdd";
        }catch (Exception e){
            request.setAttribute("msg", "添加失败！");
            return "admin/AdminAdd";
        }
    }

    /*
     * @description:根据ID删除
     * @author: mty
     */
    @RequestMapping("/adminDel/{id}")
    public ModelAndView adminDel(@PathVariable("id") String id,HttpServletRequest reuqest, Model model) throws Exception{
        ModelAndView mv = new ModelAndView();
        adminService.deleteById(id);
        mv.setViewName("admin/AdminIndex");
        return mv;
    }


}